package com.derek.omshopping.ui.home.view;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.derek.omshopping.R;
import com.derek.omshopping.base.BaseFragment;
import com.derek.omshopping.bean.home.HomeIndexListAllBean;
import com.derek.omshopping.bean.home.HomeMainTypeBean;
import com.derek.omshopping.bean.home.HomeVideoBean;
import com.derek.omshopping.ui.home.adapter.FragmentHomeTypeAdapter;
import com.derek.omshopping.ui.home.present.FragmentMainTypePresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by duyb on 2016-10-17.
 * Desc:首页fragment
 */

public class ShouYeFragment extends BaseFragment implements IFragmentMainView {
    @BindView(R.id.home_recycleview_type)
    RecyclerView homeRecycleviewType;
    @BindView(R.id.swiperefreshlayout)
    SwipeRefreshLayout swiperefreshlayout;
    @BindView(R.id.cool_rv)
    RecyclerView coolRv;
    @BindView(R.id.cool_fab)
    FloatingActionButton coolFab;
    @BindView(R.id.cool_content)
    CoordinatorLayout coolContent;
    private Context context;
    private FragmentMainTypePresenter presenter;
    private FragmentHomeTypeAdapter typeAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    @Override
    protected void initData() {
        typeAdapter = new FragmentHomeTypeAdapter(context);
        mLinearLayoutManager = new LinearLayoutManager(context);
        homeRecycleviewType.setLayoutManager(mLinearLayoutManager);
        //实例化 中间人对象并调用函数
        presenter = new FragmentMainTypePresenter(this);
        //presenter.loadData();
        // presenter.loadVideoData();
        // presenter.loadIndexData();
        presenter.loadAllData(context);
    }

    @Override
    protected void initView() {
        context = getActivity();
        //设置SwipeRefreshLayout属性
        initSwipRefresh();
        initsilde();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_maintab_type;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    private void initSwipRefresh() {
        //设置刷新时动画的颜色  可以设置4个
        swiperefreshlayout.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light
        );
        //下拉刷新
        swiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // presenter.loadData();
                //  presenter.loadVideoData();
                // presenter.loadIndexData();
                presenter.loadAllData(context);
                typeAdapter.notifyDataSetChanged();
                swiperefreshlayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void sendToHomeTypeAdapter(List<HomeMainTypeBean> list) {
        if (list != null && list.size() != 0) {
            typeAdapter.setData(list);
            homeRecycleviewType.setAdapter(typeAdapter);
        }
    }

    //首页视屏数据
    @Override
    public void sendToVideoAdapter(List<HomeVideoBean> list) {
        if (list != null && list.size() != 0) {
            typeAdapter.setVideoData(list);
            typeAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void sendToIndexAdapter(List<HomeIndexListAllBean> list) {
        if (list != null && list.size() != 0) {
            typeAdapter.setIndexData(list);
            typeAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showErroRToast(String msg) {

    }

    //滑动隐藏和显示FAB
    private void initsilde() {
        homeRecycleviewType.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //防止刷新与recycleview时间冲突
                swiperefreshlayout.setEnabled(mLinearLayoutManager.findFirstVisibleItemPosition() == 0);
                boolean isSignificantDelta = Math.abs(dy) > 0;
                if (isSignificantDelta) {
                    if (dy > 0) {
                        onScrollUp();
                    } else {
                        onScrollDown();
                    }
                }
            }

            private void onScrollDown() {

                coolFab.setVisibility(View.VISIBLE);
            }

            private void onScrollUp() {
                coolFab.setVisibility(View.GONE);
            }
        });

        coolFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mLinearLayoutManager.scrollToPositionWithOffset(0, 0);
            }
        });
    }


}
